
# 这个文件的代码，检查reference 和candidate的格式是否正确，如果不正确就调整成正确,并输出成文件bleu_divserity所需要的形式并存储
save_path_ex = 'form_outdata'

# 该函数读取predict文件，并将其输出为所需的candidate的形式
def candidate_file_handle(predict_file,out_candidate_file):
    with open(predict_file) as candidate_f:
        print("当前的文件为：",predict_file)
        predict_lines = candidate_f.readlines()
        length = len(predict_lines)
        if length not in (800,3200):
            print("并非正常的predict文件请检查")
            return False
        else:
            if length == 800:
                print("该文件每句只输出一行")
                with open(out_candidate_file, 'w', encoding='utf-8') as out_f:
                    for line in predict_lines:
                        line = line.strip()
                        if '</s>' in line:
                            line.replace('</s>','')
                        out_f.writelines(line)

            if length == 3200:
                print("该文件每句输出四行，将以链表的形式输出")
                with open(out_candidate_file, 'w', encoding='utf-8') as out_f:
                    i_rotate = 1
                    one_src = []
                    for line in predict_lines:
                        line = line.strip()
                        if '</s>' in line:
                            line.replace('</s>','')

                        if i_rotate % 4 == 0:
                            one_src.append(line)
                            out_f.writelines(str(one_src))
                            one_src = []
                        else:
                            one_src.append(line)







# 该函数将文件weibo_src_tgt中抽取正确答案，并排列成所需要的形式。
def reference_file_handle(referenc_file,out_file):
    with open(referenc_file) as reference_f:
        read_lines = reference_f.readlines()
        print("reference文件:",referenc_file,"一共",len(read_lines),"行")

        all_tgt_disctinctsrc = []

        this_src = 'None'
        tgt_forthissrc = []
        for line in read_lines:
            src,tgt = line.strip().split()
            if src == this_src:
                tgt_forthissrc.append(tgt)
            else:
                all_tgt_disctinctsrc.append(tgt_forthissrc)

                this_src = src
                tgt_forthissrc =[tgt]
        all_tgt_disctinctsrc.append(tgt_forthissrc)
    print("即将输出",len(all_tgt_disctinctsrc),"行")
    with open(out_file,'w',encoding= 'utf-8') as out_f:
        for tgt_list in all_tgt_disctinctsrc:
            out_f.writelines(str(tgt_list))
    print("已输出reference到文件：",out_file)
    return all_tgt_disctinctsrc



